package com.ruoyi.business.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.constant.TradeConstants;
import com.ruoyi.business.domain.BusinessTradeRecord;
import com.ruoyi.business.dto.RateStatisticsDTO;
import com.ruoyi.business.dto.TicketDTO;
import com.ruoyi.business.enumType.PSIceMarket;
import com.ruoyi.business.publisher.MessagePublisher;
import com.ruoyi.business.service.IBusinessTradeRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 交易记录Controller
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/business/record")
public class BusinessTradeRecordController extends BaseController {
    @Autowired
    private IBusinessTradeRecordService businessTradeRecordService;

    @Autowired
    private MessagePublisher messagePublisher;


    @PostMapping("/publish")
    public String publishMessage(@RequestBody List<TicketDTO> ticketDTOS) {
        messagePublisher.publish(JSON.toJSONString(ticketDTOS));
        return "Message published successfully";
    }


    /**
     * 查询交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessTradeRecord businessTradeRecord) {
        startPage();
        List<BusinessTradeRecord> list = businessTradeRecordService.selectBusinessTradeRecordList(businessTradeRecord);
        return getDataTable(list);
    }

    /**
     * 查询交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/winRateStatistics/{type}")
    public RateStatisticsDTO winRateStatistics(@PathVariable("type") String type) {
        return businessTradeRecordService.winRateStatistics(type);
    }

    /**
     * 导出交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessTradeRecord businessTradeRecord) {
        List<BusinessTradeRecord> list = businessTradeRecordService.selectBusinessTradeRecordList(businessTradeRecord);
        ExcelUtil<BusinessTradeRecord> util = new ExcelUtil<BusinessTradeRecord>(BusinessTradeRecord.class);
        util.exportExcel(response, list, "交易记录数据");
    }

    /**
     * 获取交易记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(businessTradeRecordService.selectBusinessTradeRecordById(id));
    }

    /**
     * 新增交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "交易记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTradeRecord businessTradeRecord) {

        if (businessTradeRecord.getExchange().equals(PSIceMarket.SH.value())) {
            businessTradeRecord.setStockCode(TradeConstants.SYMBOL_PREFIX_SHSE + "." + businessTradeRecord.getStockCode());
        } else  if (businessTradeRecord.getExchange().equals(PSIceMarket.SZ.value())) {
            businessTradeRecord.setStockCode(TradeConstants.SYMBOL_PREFIX_SZSE + "." + businessTradeRecord.getStockCode());
        }
        return businessTradeRecordService.tradeAndInsertBusinessTradeRecord(businessTradeRecord);
    }

    /**
     * 撤单
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "撤单", businessType = BusinessType.UPDATE)
    @PostMapping("/cancle")
    public AjaxResult cancleOrder(@RequestBody BusinessTradeRecord businessTradeRecord) {
        return businessTradeRecordService.cancleOrder(businessTradeRecord);
    }


    /**
     * 修改交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "交易记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessTradeRecord businessTradeRecord) {
        return toAjax(businessTradeRecordService.updateBusinessTradeRecord(businessTradeRecord));
    }

    /**
     * 删除交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "交易记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(businessTradeRecordService.deleteBusinessTradeRecordByIds(ids));
    }
}
